<?php

namespace App\Traits;

use App\Models\Sys_Params;
use Carbon\Carbon;

class External
{
    private static $config;

    private function __construct()
    {
        // 初始化代码
    }

    public static function getConfig()
    {
        if (is_null(self::$config)) {
            self::$config = Sys_Params::get()->toArray();
        }
        return self::$config;
    }

    public static function getParams($field)
    {

        $config = self::getConfig();
        $found_key = array_search($field, array_column($config, 'itemcode'));
        return json_decode($config[$found_key]['itemvalue'],true);
    }

    public static function optionResult($result,$option)
    {
        //$option:1为主动方信息如浏览者；2为被动方信息如被浏览者；3为双方信息
        $arr = [];
        if ($option == 1){
            foreach ($result as  $key => $item){
                $arr[$key] = $item->toArray();
                if(!empty($item->mid)){
                    $arr[$key]['base']['id'] = $item->hasOneMember->id;
                    $arr[$key]['base']['username'] = $item->hasOneMember->username;
                    $arr[$key]['base']['realname'] = $item->hasOneMember->realname;
                    $arr[$key]['base']['avatar'] = $item->hasOneMember->avatar;
                    $arr[$key]['base']['typeText'] = $item->hasOneMember->typeText;
                    $arr[$key]['base']['sn'] = $item->hasOneMember->sn;
                    $arr[$key]['base']['nationalText'] = $item->hasOneMember->nationalText;
                    $arr[$key]['base']['age'] = $item->hasOneMember->age;
                    $arr[$key]['base']['height'] = $item->hasOneMember->height;
                    $arr[$key]['base']['educationText'] = $item->hasOneMember->educationText;
                    $arr[$key]['base']['marryText'] = $item->hasOneMember->marryText;
                    $arr[$key]['base']['genderText'] = $item->hasOneMember->genderText;
                    $arr[$key]['base']['belongText'] = $item->hasOneMember->belongText;
                }else{
                    $arr[$key]['base'] = [];
                }

            }
        }
        if ($option == 2){
            foreach ($result as  $key => $item){
                $arr[$key] = $item->toArray();
                if(!empty($item->otherid)){
                    $arr[$key]['base']['id'] = $item->hasOneOtherMember->id;
                    $arr[$key]['base']['username'] = $item->hasOneOtherMember->username;
                    $arr[$key]['base']['realname'] = $item->hasOneOtherMember->realname;
                    $arr[$key]['base']['avatar'] = $item->hasOneOtherMember->avatar;
                    $arr[$key]['base']['typeText'] = $item->hasOneOtherMember->typeText;
                    $arr[$key]['base']['sn'] = $item->hasOneOtherMember->sn;
                    $arr[$key]['base']['nationalText'] = $item->hasOneOtherMember->nationalText;
                    $arr[$key]['base']['age'] = $item->hasOneOtherMember->age;
                    $arr[$key]['base']['height'] = $item->hasOneOtherMember->height;
                    $arr[$key]['base']['educationText'] = $item->hasOneOtherMember->educationText;
                    $arr[$key]['base']['marryText'] = $item->hasOneOtherMember->marryText;
                    $arr[$key]['base']['genderText'] = $item->hasOneOtherMember->genderText;
                    $arr[$key]['base']['belongText'] = $item->hasOneOtherMember->belongText;
                }else{
                    $arr[$key]['base'] = [];
                }

            }
        }
        if ($option == 3){
            foreach ($result as  $key => $item){
                $arr[$key] = $item->toArray();
                if(!empty($item->mid)) {
                    $arr[$key]['base']['id'] = $item->hasOneMember->id;
                    $arr[$key]['base']['username'] = $item->hasOneMember->username;
                    $arr[$key]['base']['realname'] = $item->hasOneMember->realname;
                    $arr[$key]['base']['avatar'] = $item->hasOneMember->avatar;
                    $arr[$key]['base']['typeText'] = $item->hasOneMember->typeText;
                    $arr[$key]['base']['sn'] = $item->hasOneMember->sn;
                    $arr[$key]['base']['nationalText'] = $item->hasOneMember->nationalText;
                    $arr[$key]['base']['age'] = $item->hasOneMember->age;
                    $arr[$key]['base']['height'] = $item->hasOneMember->height;
                    $arr[$key]['base']['educationText'] = $item->hasOneMember->educationText;
                    $arr[$key]['base']['marryText'] = $item->hasOneMember->marryText;
                    $arr[$key]['base']['genderText'] = $item->hasOneMember->genderText;
                    $arr[$key]['base']['belongText'] = $item->hasOneMember->belongText;
                }else{
                    $arr[$key]['base'] = [];
                }

                if(!empty($item->otherid)) {
                    $arr[$key]['obase']['id'] = $item->hasOneOtherMember->id;
                    $arr[$key]['obase']['username'] = $item->hasOneOtherMember->username;
                    $arr[$key]['obase']['realname'] = $item->hasOneOtherMember->realname;
                    $arr[$key]['obase']['avatar'] = $item->hasOneOtherMember->avatar;
                    $arr[$key]['obase']['typeText'] = $item->hasOneOtherMember->typeText;
                    $arr[$key]['obase']['sn'] = $item->hasOneOtherMember->sn;
                    $arr[$key]['obase']['nationalText'] = $item->hasOneOtherMember->nationalText;
                    $arr[$key]['obase']['age'] = $item->hasOneOtherMember->age;
                    $arr[$key]['obase']['height'] = $item->hasOneOtherMember->height;
                    $arr[$key]['obase']['educationText'] = $item->hasOneOtherMember->educationText;
                    $arr[$key]['obase']['marryText'] = $item->hasOneOtherMember->marryText;
                    $arr[$key]['obase']['genderText'] = $item->hasOneOtherMember->genderText;
                    $arr[$key]['obase']['belongText'] = $item->hasOneOtherMember->belongText;
                }else{
                    $arr[$key]['obase'] = [];
                }
            }
        }

        return $arr;
    }

    public static function optionItem($item)
    {
        $arr = [];
        foreach ($item as $key => $value)
        {
            $arr[$key]['id'] = $value['id'];
            if(isset($value['otherid']) && empty($value['otherid'])){

                $arr[$key] = $value;
                $arr[$key]['base'] = [];
                continue;
            }
//            print_r($value->toArray());exit;
            foreach ($value as $k => $v){

                if(
                    $k == 'id'|| $k == 'username' || $k == 'realname' || $k == 'avatar' ||
                    $k == 'typeText' || $k == 'sn' || $k == 'nationalText' || $k == 'age' ||
                    $k == 'height' || $k == 'educationText' || $k == 'marryText' || $k == 'genderText'
                ){
                    $arr[$key]['base'][$k] = $v;
                }else{
                    $arr[$key][$k] = $v;
                }

            }

        }
        return $arr;
    }

    public static function getAge($birthday, $now_time)
    {
        list($_year, $_mouth, $day) = explode("-", $birthday);
        $data = Carbon::createFromDate($_year, $_mouth, $day);
        $year = $data->diffInYears($now_time);
        $month = $data->diffInMonths(Carbon::parse('-' . $year . ' year')->toDateTimeString());
        $day = $data->diffInDays(Carbon::parse('-' . (($year * 12) + $month) . ' months')->toDateTimeString());
        $month_age = $data->diffInMonths($now_time);
        $day_age = $data->diffInDays($now_time);
        return [
            'age' => [
                'year' => $year,
                'month' => $month,
                'day' => $day,
            ],
            'total_age' => [
                'year' => $year,
                'month' => $month_age,
                'day' => $day_age,
            ]
        ];
    }

    /**
     * 根据生日获取生肖
     *
     * Author:kayson
     * */
    public static function zodiac($birthday)
    {
        $DOB = date("Y", strtotime($birthday));
        $animals = array(
            '鼠', '牛', '虎', '兔', '龙', '蛇',
            '马', '羊', '猴', '鸡', '狗', '猪'
        );
        $key = ($DOB - 1900) % 12;
        return $animals[$key];
    }

    /**
     * 根据生日获取星座
     *
     * Author:kayson
     * */
    public static function constellation($birthday)
    {
        $DOB = date("m-d", strtotime($birthday));
        list($month,$day) = explode("-",$DOB);
        $xingzuo = '';
        // 检查参数有效性
        if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
        {
            return $xingzuo;
        }

        if(($month == 1 && $day >= 20) || ($month == 2 && $day <= 18)) {
            $xingzuo = "水瓶座";
        }else if(($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) {
            $xingzuo = "双鱼座";
        }else if (($month == 3 && $day >= 21) || ($month == 4 && $day <= 19)) {
            $xingzuo = "白羊座";
        }else if (($month == 4 && $day >= 20) || ($month == 5 && $day <= 20)) {
            $xingzuo = "金牛座";
        }else if (($month == 5 && $day >= 21) || ($month == 6 && $day <= 21)) {
            $xingzuo = "双子座";
        }else if (($month == 6 && $day >= 22) || ($month == 7 && $day <= 22)) {
            $xingzuo = "巨蟹座";
        }else if (($month == 7 && $day >= 23) || ($month == 8 && $day <= 22)) {
            $xingzuo = "狮子座";
        }else if (($month == 8 && $day >= 23) || ($month == 9 && $day <= 22)) {
            $xingzuo = "处女座";
        }else if (($month == 9 && $day >= 23) || ($month == 10 && $day <= 23)) {
            $xingzuo = "天秤座";
        }else if (($month == 10 && $day >= 24) || ($month == 11 && $day <= 22)) {
            $xingzuo = "天蝎座";
        }else if (($month == 11 && $day >= 23) || ($month == 12 && $day <= 21)) {
            $xingzuo = "射手座";
        }else if (($month == 12 && $day >= 22) || ($month == 1 && $day <= 19)) {
            $xingzuo = "摩羯座";
        }

        return $xingzuo;
    }
}
